package com.xisoft.devise.controller;

import com.xisoft.common.aspect.AppMonitorRuning;
import com.xisoft.common.pubEnv.LoggerUtil;
import com.xisoft.common.result.Result;
import com.xisoft.devise.service.IMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * *
 * Created with Description:
 *
 * @version 1.0 <br>
 * @packagename: com.xisoft.devise.controller<br>
 * @author: LiBencheng <br>
 * @computer: licheng <br>
 * @date: Created on 2022/8/25 18:23 <br>
 */
@RestController
@RequestMapping ("/meta")
public class MetadataController
{
    @Qualifier ("MetadataServiceImpl")
    @Autowired
    IMetadataService service;

    @PostMapping ("/savaSysDataCode")
    @AppMonitorRuning (method = "MetadataController.savaSysDataCode", funEffect = "保存方案savaSysDataCode！")
    public Result savaSysDataCode(String json, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        try
        {
            return Result.ok(service.savaSysDataCode(json));
        }
        catch (Exception e)
        {
            LoggerUtil.getInstance().exception("保存方案失败！", e);
            return Result.fail().message(e.getMessage());
        }
    }

    @PostMapping ("/savaScript")
    @AppMonitorRuning (method = "MetadataController.savaScript", funEffect = "保存元数据并生成SQLScript！")
    public Result savaScript(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        try
        {
            return Result.ok(service.savaScript(request.getParameter("metajson")));
        }
        catch (Exception e)
        {
            LoggerUtil.getInstance().exception("保存元数据并生成SQLScript失败！", e);
            return Result.fail().message(e.getMessage());
        }
    }

    //The valid characters are defined in RFC 7230 and RFC 3986
    @GetMapping ("/getSysMeta")
    @AppMonitorRuning (method = "MetadataController.getSysMeta", funEffect = "getSysMeta！")
    public Result getMetadataInfo(HttpServletRequest request, HttpServletResponse response) throws Exception
    { return Result.ok(service.getMetadataInfo()); }


    @GetMapping ("/getSysTable")
    @AppMonitorRuning (method = "MetadataController.getSysTable", funEffect = "根据系统查询表名！")
    public Result getDicInfo(HttpServletRequest request, HttpServletResponse response) throws Exception
    { return Result.ok(service.getSysTable(request.getParameter("syscode"))); }

    @GetMapping ("/getSysTableField")
    @AppMonitorRuning (method = "MetadataController.getSysTableField", funEffect = "根据表名查询对应的字段名！")
    public Result getSysDicInfo(HttpServletRequest request, HttpServletResponse response) throws Exception
    { return Result.ok(service.getSysTableField(request.getParameter("tablename"))); }

    @GetMapping ("/getTableInfo")
    @AppMonitorRuning (method = "MetadataController.getTableInfo", funEffect = "根据系统查询表名！")
    public Result getSysTable(HttpServletRequest request, HttpServletResponse response) throws Exception
    { return Result.ok(service.getSysTableByCode(request.getParameter("table"))); }

    @GetMapping ("/getSysTabByTable")
    @AppMonitorRuning (method = "MetadataController.getSysTabByTable", funEffect = "根据表名查询表信息！")
    public Result getSysTabByTable(HttpServletRequest request, HttpServletResponse response) throws Exception
    { return Result.ok(service.getSysTabByTable(request.getParameter("table"))); }

}
